<%@ page contentType="text/html;charset=UTF-8" %>
<script>
    $(document).ready(function() {
    $('#dispatchTable').bootstrapTable({

        //请求方法
        method: 'get',
        //类型json
        dataType: "json",
        //显示刷新按钮
        showRefresh: true,
        //显示切换手机试图按钮
        showToggle: true,
        //显示 内容列下拉框
        showColumns: true,
        //显示到处按钮
        showExport: true,
        //显示切换分页按钮
        showPaginationSwitch: true,
        //显示详情按钮
        detailView: true,
        //显示详细内容函数
        detailFormatter: "detailFormatter",
        //最低显示2行
        minimumCountColumns: 2,
        //是否显示行间隔色
        striped: true,
        //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        cache: false,
        //是否显示分页（*）
        pagination: true,
        //排序方式
        sortOrder: "asc",
        //初始化加载第一页，默认第一页
        pageNumber:1,
        //每页的记录行数（*）
        pageSize: 10,
        //可供选择的每页的行数（*）
        pageList: [10, 25, 50, 100],
        //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据
        url: "${ctx}/tms/dispatch/dispatchDistributionData",
        //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
        //queryParamsType:'',
        ////查询参数,每次调用是会带上这个参数，可自定义
        queryParams : function(params) {
            var searchParam = $("#searchForm").serializeJSON();
            searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
            searchParam.pageSize = params.limit === undefined? -1 : params.limit;
            searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
            return searchParam;
        },
        //分页方式：client客户端分页，server服务端分页（*）
        sidePagination: "server",
        contextMenuTrigger:"right",//pc端 按右键弹出菜单
        contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
        contextMenu: '#context-menu',
        onContextMenuItem: function(row, $el){
            if($el.data("item") == "edit"){
                edit(row.id);
            } else if($el.data("item") == "delete"){
                jp.confirm('确认要删除该调度单记录吗？', function(){
                    jp.loading();
                    jp.get("${ctx}/tms/dispatch/delete?id="+row.id, function(data){
                        if(data.success){
                            $('#dispatchTable').bootstrapTable('refresh');
                            jp.success(data.msg);
                        }else{
                            jp.error(data.msg);
                        }
                    })

                });

            }
        },

        onClickRow: function(row, $el){
        },
        columns: [{
            checkbox: true

        }
            ,{
                field: 'code',
                title: '调度单编号',
                sortable: true
                ,formatter:function(value, row , index){
                    return "<a href='javascript:edit(\""+row.id+"\")'>"+value+"</a>";
                }

            }
            ,{
                field: 'dispatchDDate',
                title: '调度时间',
                sortable: true

            }
            ,{
                field: 'fetchDate',
                title: '取货时间',
                sortable: true

            }
            ,{
                field: 'truckNumber',
                title: '车牌号',
                sortable: true

            }
            ,{
                field: 'driver',
                title: '司机',
                sortable: true

            }
            ,{
                field: 'driverPhone',
                title: '司机电话',
                sortable: true

            }
            ,{
                field: 'truckSurplusWeight',
                title: '车辆剩余空间重量',
                sortable: true

            }
            ,{
                field: 'truckSurplusVolume',
                title: '车辆剩余方量',
                sortable: true

            }
            ,{
                field: 'totalWeight',
                title: '总重量',
                sortable: true

            }
            ,{
                field: 'totalVolume',
                title: '总方数',
                sortable: true

            }
            ,{
                field: 'freightCost',
                title: '运费成本',
                sortable: true

            }
            ,{
                field: 'remarks',
                title: '备注信息',
                sortable: true

            }
        ]

    });


    if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端


    $('#dispatchTable').bootstrapTable("toggleView");
}

    $('#dispatchTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
    'check-all.bs.table uncheck-all.bs.table', function () {
    $('#remove').prop('disabled', ! $('#dispatchTable').bootstrapTable('getSelections').length);
    $('#edit').prop('disabled', $('#dispatchTable').bootstrapTable('getSelections').length!=1);
});

    $("#btnImport").click(function(){
    jp.open({
    type: 1,
    area: [500, 300],
    title:"导入数据",
    content:$("#importBox").html() ,
    btn: ['下载模板','确定', '关闭'],
    btn1: function(index, layero){
    window.location='${ctx}/tms/dispatch/import/template';
},
    btn2: function(index, layero){
    var inputForm =top.$("#importForm");
    var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe
    inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
    inputForm.onsubmit = function(){
    jp.loading('  正在导入，请稍等...');
}
    inputForm.submit();
    jp.close(index);
},

    btn3: function(index){
    jp.close(index);
}
});
});

    $("#search").click("click", function() {// 绑定查询按扭
    $('#dispatchTable').bootstrapTable('refresh');
});

    $("#reset").click("click", function() {// 绑定查询按扭
    $("#searchForm  input").val("");
    $("#searchForm  select").val("");
    $("#searchForm  .select-item").html("");
    $('#dispatchTable').bootstrapTable('refresh');
});

    $('#dispatchDDate').datetimepicker({
    format: "YYYY-MM-DD HH:mm:ss"
});

});

    function getIdSelections() {
    return $.map($("#dispatchTable").bootstrapTable('getSelections'), function (row) {
    return row.id
});
}

    function deleteAll(){

    jp.confirm('确认要删除该调度单记录吗？', function(){
        jp.loading();
        jp.get("${ctx}/tms/dispatch/deleteAll?ids=" + getIdSelections(), function(data){
            if(data.success){
                $('#dispatchTable').bootstrapTable('refresh');
                jp.success(data.msg);
            }else{
                jp.error(data.msg);
            }
        })

    })
}

    function add(){
    jp.openDialog('新增调度单', "${ctx}/tms/dispatch/distributionform",'800px', '500px', $('#dispatchTable'));
}

    function edit(id){//没有权限时，不显示确定按钮
    if(id == undefined){
    id = getIdSelections();
}
    <shiro:hasPermission name="tms:dispatch:edit">
    jp.openDialog('编辑调度单', "${ctx}/tms/dispatch/distributionform?id=" + id,'800px', '500px', $('#dispatchTable'));
    </shiro:hasPermission>
    <shiro:lacksPermission name="tms:dispatch:edit">
    jp.openDialogView('查看调度单', "${ctx}/tms/dispatch/distributionform?id=" + id,'800px', '500px', $('#dispatchTable'));
    </shiro:lacksPermission>
    }





    function detailFormatter(index, row) {
    var htmltpl =  $("#dispatchChildrenTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
    var html = Mustache.render(htmltpl, {
    idx:row.id
});
    $.get("${ctx}/tms/dispatch/detail?id="+row.id, function(dispatch){
    var dispatchChild1RowIdx = 0, dispatchChild1Tpl = $("#dispatchChild1Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
    var data1 =  dispatch.dispatchAppendList;
    for (var i=0; i<data1.length; i++){
    addRow('#dispatchChild-'+row.id+'-1-List', dispatchChild1RowIdx, dispatchChild1Tpl, data1[i]);
    dispatchChild1RowIdx = dispatchChild1RowIdx + 1;
}

    var dispatchChild2RowIdx = 0, dispatchChild2Tpl = $("#dispatchChild2Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
    var data2 =  dispatch.dispatchDetailList;
    for (var i=0; i<data2.length; i++){
    addRow('#dispatchChild-'+row.id+'-2-List', dispatchChild2RowIdx, dispatchChild2Tpl, data2[i]);
    dispatchChild2RowIdx = dispatchChild2RowIdx + 1;
}


})

    return html;
}

    function addRow(list, idx, tpl, row){
    $(list).append(Mustache.render(tpl, {
        idx: idx, delBtn: true, row: row
    }));
}

</script>
<script type="text/template" id="dispatchChildrenTpl">//<!--
	<div class="tabs-container">
		<ul class="nav nav-tabs">
				<li class="active"><a data-toggle="tab" href="#tab-{{idx}}-1" aria-expanded="true">附加费用</a></li>
				<li><a data-toggle="tab" href="#tab-{{idx}}-2" aria-expanded="true">订单明细</a></li>
		</ul>
		<div class="tab-content">
				 <div id="tab-{{idx}}-1" class="tab-pane fade in active">
						<table class="ani table">
						<thead>
							<tr>
								<th>费用描述</th>
								<th>费用金额</th>
								<th>类型</th>
								<th>备注信息</th>
							</tr>
						</thead>
						<tbody id="dispatchChild-{{idx}}-1-List">
						</tbody>
					</table>
				</div>
				<div id="tab-{{idx}}-2" class="tab-pane fade">
					<table class="ani table">
						<thead>
							<tr>
								<th>订单编号</th>
								<th>提货地址</th>
								<th>收货地址</th>
								<th>包装规格</th>
								<th>重量（KG）</th>
								<th>方数（m³）</th>
								<th>备注信息</th>
							</tr>
						</thead>
						<tbody id="dispatchChild-{{idx}}-2-List">
						</tbody>
					</table>
				</div>
		</div>//-->
</script>
<script type="text/template" id="dispatchChild1Tpl">//<!--
				<tr>
					<td>
						{{row.append}}
					</td>
					<td>
						{{row.appendCharge}}
					</td>
					<td>
						{{row.appendType}}
					</td>
					<td>
						{{row.remarks}}
					</td>
				</tr>//-->
</script>
<script type="text/template" id="dispatchChild2Tpl">//<!--
				<tr>
					<td>
						{{row.orderCode}}
					</td>
					<td>
						{{row.pickAddress}}
					</td>
					<td>
						{{row.receiveAddress}}
					</td>
					<td>
						{{row.packaging}}
					</td>
					<td>
						{{row.weight}}
					</td>
					<td>
						{{row.volume}}
					</td>
					<td>
						{{row.remarks}}
					</td>
				</tr>//-->
</script>
